import Vue from 'vue'
import VueRouter from 'vue-router'

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  // @ts-ignore
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)
const routes = [
  {
      path:"/",
      redirect:"/login"
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/pages/Home.vue'),
    redirect:"/welcome",
    children:[
        {
            path:"/welcome",
            name:"welcome",
            component: () =>
            import ('@/views/pages/Welcome.vue'),
        },
        {
          path: '/users',
          name: 'users',
          component: () =>import('@/views/user/Users.vue')
        },
        {
          path: '/roles',
          name: 'roles',
          component: () =>import('@/views/role/Roles.vue')
        },
        {
          path: '/rights',
          name: 'rights',
          component: () =>import('@/views/role/Rights.vue')
        },
        {
          path: '/goods',
          name: 'goods',
          component: () =>import('@/views/goods/Goods.vue')
        },
        {
          path: '/params',
          name: 'params',
          component: () =>import('@/views/goods/Params.vue')
        },
        {
          path: '/categories',
          name: 'categories',
          component: () =>import('@/views/goods/Categories.vue')
        },
        {
          path: '/orders',
          name: 'orders',
          component: () =>import('@/views/orders/Orders.vue')
        },
        {
          path: '/folder',
          name: 'folder',
          component: () =>import('@/views/folder/Folder.vue')
        },
        {
          path: '/reports',
          name: 'reports',
          component: () =>import('@/views/reports/Reports.vue')
        }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () =>import('@/views/pages/Login.vue')
  }
]
const router = new VueRouter({
  routes
})
//路由导航守卫
router.beforeEach((to,from,next)=>{
  if(to.path=="/login") return next();
  const token=window.sessionStorage.getItem("token");
  if(!token) return next('/login');
  next();
})
export default router
